﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Exhibition.Dao.ViewModel;
using AutoMapper;
namespace Exhibition.Dao
{

    public class BadgesDao
    {


        public BadgesDao()
        {

            Mapper.CreateMap<E_Badges, BadgesModel>();
        }
        public List<E_Badges> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                recordcount = edm.E_Badges.Count();

                return edm.E_Badges.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public BadgesModel GetOneById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Badges.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<E_Badges, BadgesModel>(query);
            }

        }


        public int Save(BadgesModel obj)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Badges.FirstOrDefault(s => s.Id == obj.Id);

                if (query == null)
                {


                    query = new E_Badges();
                    query.CreateTime = obj.CreateTime;

                    query.CreateUserId = obj.CreateUserId;

                    query.CreateUserName = obj.CreateUserName;

                    query.SiteId = obj.SiteId;
                    edm.AddToE_Badges(query);

                }


                query.CompName = obj.CompName;

                query.RealName = obj.RealName;

                query.Position = obj.Position;

                query.UserLogo = obj.UserLogo;

               
                edm.SaveChanges();

                return query.Id;


            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="siteid"></param>
        /// <returns></returns>
        public List<BadgesModel> GetBadList(int userid, int siteid)
        {
            using (ExhibitionEntities edm = new ExhibitionEntities())
            {
                var query = edm.E_Badges.Where(s=>s.CreateUserId==userid && s.SiteId==siteid);
                return Mapper.Map<List<E_Badges>, List<BadgesModel>>(query.ToList());
            }
        }

        public int DelById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Badges.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

